/*
 *  Copyright (C) 2009  Peter Kist & Jan Ripke
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "KeyMapper.h"

#include <fstream>

#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

#include <Core/Kernel/AppContext.h>

namespace galaxy {
	namespace input {
		namespace ois_input {

			KeyMapper::KeyMapper () {
				m_DefaultAction = INPUT_KEYACTION_KEY;
			}

			KeyMapper::~KeyMapper () {
			}

			bool KeyMapper::loadKeyMap (std::string& mapName) {
                std::string mapPath = getKeyMapPath (mapName);
				std::ifstream ifs(mapPath.c_str());

                if (ifs.good()) {
                    boost::archive::xml_iarchive ia(ifs);

                    int size;
                    ia >> BOOST_SERIALIZATION_NVP(size);

                    for (int i = 0; i < size; i++) {
                        KeyMapDefinition keydef;
                        ia >> BOOST_SERIALIZATION_NVP(keydef);

                        MappedKey mappedKey(keydef.m_KeyCode,keydef.m_Modifier);
                        linkMappedKeyToAction(mappedKey, (KeyAction) keydef.m_Action);
                    }
                    return true;
                }
				return false;
			}

			bool KeyMapper::saveKeyMap (std::string& mapName) {
				std::string mapPath = getKeyMapPath (mapName);
				std::ofstream ofs(mapPath.c_str());
				
				if (ofs.good()) {
					boost::archive::xml_oarchive oa(ofs);

                    int size = m_KeyMap.size ();
                    oa << BOOST_SERIALIZATION_NVP (size);

					KeyMap::iterator iter = m_KeyMap.begin ();
					while (iter != m_KeyMap.end ()) {
						const KeyMapDefinition keydef(1,2,3);
						oa << BOOST_SERIALIZATION_NVP(keydef);
						iter++;
					}
                    return true;
				}
				return false;
			}

			
			bool KeyMapper::linkMappedKeyToAction (MappedKey mappedKey, KeyAction keyAction) {
				m_KeyMap[mappedKey] = keyAction;
				return true;
			}

			KeyAction KeyMapper::getAction (MappedKey& mappedKey) {
				KeyMap::iterator iter = m_KeyMap.find (mappedKey);
				if (iter != m_KeyMap.end ()) {
					return (*iter).second;
				}
				return m_DefaultAction;
			}

			KeyAction KeyMapper::getAction (int keyCode, int modifiers) {
				MappedKey mappedKey(keyCode, modifiers);
				return getAction (mappedKey);
			}

			std::string KeyMapper::getKeyMapPath (std::string& mapName) {
				AppContext& context = AppContext::singleton();

				if (mapName.find_last_of (".keymap") == std::string::npos) {
					mapName += ".keymap";
				}

				return (context.paths.keymaps / mapName).string(); 
			}

		} // namespace ois_input
	} // namespace input
} // namespace galaxy
